package com.xinchengxd.chat;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.xinchengxd.commons.ResponseResult;

import javax.websocket.EncodeException;
import javax.websocket.Encoder;
import javax.websocket.EndpointConfig;

/**
 * @author: 姚新成
 * @Date: 2021/11/17:17:10
 * @Description: 统一响应的编码器
 */

public class MyEncode implements Encoder.Text<ResponseResult> {
    @Override
    public String encode(ResponseResult setResponseResult) throws EncodeException {
        ObjectMapper objectMapper = new ObjectMapper();
        String s = null;
        try {
             s = objectMapper.writeValueAsString(setResponseResult);
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
        return s;
    }

    @Override
    public void init(EndpointConfig endpointConfig) {

    }

    @Override
    public void destroy() {

    }
}
